from django.conf import settings
from django.db import models

from rbac.models import UserInfo as RbacUserInfo


class Depart(models.Model):
    """
    部门表
    """
    title = models.CharField(verbose_name='部门名称', max_length=32, unique=True)

    def __str__(self):
        return self.title


class UserInfo(RbacUserInfo):
    """
    员工表
    """
    nickname = models.CharField(verbose_name='姓名', max_length=16, unique=True, help_text='真实姓名')
    phone = models.CharField(verbose_name='手机', max_length=32, null=True, blank=True)
    gender_choices = (
        (1, '男'),
        (2, '女')
    )
    gender = models.IntegerField(verbose_name='性别', choices=gender_choices, default=1)
    depart = models.ForeignKey(verbose_name='部门', to=Depart, on_delete=models.CASCADE)
    email = models.EmailField(verbose_name='邮箱', blank=True, null=True)
    is_depart = models.BooleanField(verbose_name='部门是否生效', default=False)

    def __str__(self):
        return self.nickname


class IpScope(models.Model):
    """
    网络范围
    """
    ip_net = models.CharField(verbose_name='IP地址范围', unique=True, max_length=32,
                              help_text='网络地址格式:10.207.0.0/16,10.207.0.0/255.255.0.0')
    is_chart = models.BooleanField(verbose_name='是否显示图表', default=True)
    service_type = models.ManyToManyField(verbose_name='业务类型', to='ServiceType')

    def __str__(self):
        return self.ip_net


class ServiceType(models.Model):
    """
    业务类型
    """
    title = models.CharField(verbose_name='业务名称', unique=True, max_length=64)

    def __str__(self):
        return self.title


class AssetType(models.Model):
    """
    资产类型
    """
    title = models.CharField(verbose_name='资产类型', unique=True, max_length=64)

    def __str__(self):
        return self.title


class IpSubnet(models.Model):
    """
    子网
    """
    ip_network = models.ForeignKey(verbose_name='网段范围', to=IpScope, on_delete=models.CASCADE)
    service_type_choice = (
        (0, '待分配'),
        (1, '内部服务网'),
        (2, '外部服务网'),
        (3, '旅服网'),
        (4, 'THDS'),
        (5, '客运视频监控'),
    )
    service_type = models.ForeignKey(verbose_name='业务类型', to=ServiceType, on_delete=models.SET_NULL, null=True,
                                     default=settings.UNUSE_SERVICE_TYPE)
    app_description = models.CharField(verbose_name='子网应用类型描述', max_length=64, null=True, blank=True)
    subnet = models.CharField(verbose_name='子网号', max_length=64, unique=True)
    subnet_num = models.BigIntegerField(verbose_name='子网号(数字)', help_text='数字表示的子网号，用于排序')
    depart = models.ManyToManyField(verbose_name='使用单位', to=Depart)
    responsible_person = models.CharField(verbose_name='负责人', max_length=32, null=True, blank=True)
    location = models.CharField(verbose_name='位置', max_length=64, null=True, blank=True)
    pid = models.ForeignKey(verbose_name='与C类子网关联的ID号', to='IpSubnet', related_name='parents',
                            on_delete=models.CASCADE, null=True, blank=True)
    last_change_date = models.DateTimeField(verbose_name='变更日期', null=True, auto_now=True)

    def __str__(self):
        return '%s-网关位置：%s' % (self.subnet, '' if self.location == None else self.location)


class IpSubnetRecord(models.Model):
    """
    子网分配记录
    """
    ip_subnet = models.ForeignKey(verbose_name='子网地址', to=IpSubnet, on_delete=models.CASCADE)
    subnet = models.CharField(verbose_name='子网地址', max_length=32, help_text='关键字查询使用')
    service_type_choices = (
        (0, '待分配'),
        (1, '内部服务网'),
        (2, '外部服务网'),
        (3, '旅服网'),
        (4, 'THDS'),
        (4, '客运视频监控'),
    )
    service_type = models.ForeignKey(verbose_name='业务类型', to=ServiceType, on_delete=models.SET_NULL, null=True)
    depart = models.ManyToManyField(verbose_name='使用单位', to=Depart)
    location = models.CharField(verbose_name='位置', max_length=64, null=True, blank=True)
    responsible_person = models.CharField(verbose_name='负责人', max_length=32, null=True, blank=True)
    operate_type_choices = (
        (0, '回收'),
        (1, '分配'),
    )
    operate_type = models.IntegerField(verbose_name='操作类型', choices=operate_type_choices)
    operate_person = models.ForeignKey(verbose_name='分配人', to=UserInfo, on_delete=models.CASCADE)
    operate_date = models.DateTimeField(verbose_name='分配时间', auto_now_add=True)
    note = models.TextField(verbose_name='备注')

    def __str__(self):
        return self.subnet


class Hosts(models.Model):
    """
    主机表
    """
    ip_subnet = models.ForeignKey(verbose_name='所属子网', to=IpSubnet, on_delete=models.CASCADE)
    ip_address = models.CharField(verbose_name='IP地址', max_length=32)
    ip_address_num = models.BigIntegerField(verbose_name='IP地址(数字)')
    ip_type_choices = (
        (0, '未分配'),
        (1, '网络地址'),
        (2, '广播地址'),
        (3, '服务器'),
        (4, '存储设备'),
        (5, '网络设备'),
        (6, '安全设备'),
        (7, '机房设备'),
        (8, '终端设备'),
    )
    ip_type = models.ForeignKey(verbose_name='资产类型', to=AssetType, on_delete=models.SET_NULL, null=True,
                                default=settings.UNUSE_IP_TYPE)
    depart = models.ForeignKey(verbose_name='使用单位', to=Depart, on_delete=models.SET_NULL, null=True, blank=True)
    location = models.CharField(verbose_name='位置', max_length=64, null=True, blank=True)
    responsible_person = models.CharField(verbose_name='负责人', max_length=32, null=True)
    app_name = models.CharField(verbose_name='设备名称/应用名称', max_length=128, null=True)
    last_change_date = models.DateTimeField(verbose_name='变更日期', auto_now=True)
    auto_check_method_choices = (
        (0, 'telnet'),
        (1, 'ssh'),
    )
    auto_check_method = models.IntegerField(verbose_name='自动检查方法', choices=auto_check_method_choices, default=0)
    auto_check_scipt = models.CharField(verbose_name='自动检查脚本', max_length=128, null=True, blank=True)

    def __str__(self):
        return self.ip_address


class HostRecord(models.Model):
    """
    主机分配记录
    """
    ip = models.ForeignKey(verbose_name='主机ip', to=Hosts, on_delete=models.CASCADE, null=True, blank=True)
    ip_subnet = models.ForeignKey(verbose_name='所属子网', to=IpSubnet, on_delete=models.CASCADE)
    ip_address = models.CharField(verbose_name='IP地址', max_length=32, null=True, blank=True)
    depart = models.ForeignKey(verbose_name='使用部门', to=Depart, on_delete=models.SET_NULL, null=True, blank=True)
    app_name = models.CharField(verbose_name='设备名称/应用名称', max_length=128)
    location = models.CharField(verbose_name='位置', max_length=64, null=True, blank=True)
    responsible_person = models.CharField(verbose_name='负责人', max_length=32)
    operate_person = models.ForeignKey(verbose_name='操作人', to=UserInfo, on_delete=models.CASCADE)
    operate_type_choices = (
        (0, '回收'),
        (1, '分配'),
    )
    operate_type = models.IntegerField(verbose_name='操作类型', choices=operate_type_choices)
    operate_date = models.DateTimeField(verbose_name='操作时间', auto_now_add=True)
    note = models.TextField(verbose_name='备注')


class UserOperate(models.Model):
    """
    用户操作记录
    """
    operate_person = models.CharField(verbose_name='操作人', max_length=64)
    operate_date = models.DateTimeField(verbose_name='操作时间', auto_now_add=True)
    action = models.CharField(verbose_name='操作', max_length=64)

    def __str__(self):
        return '%s:%s' % (self.operate_person, self.action)
